package com.isunland.managesystem.utils;

import android.content.Context;
import com.isunland.managesystem.R;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 日期工具类
 *
 * @author wangyanan
 */
@SuppressWarnings("all") public class DateUtil {
  private static DateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
  private static DateFormat sFormatSimple;
  public static final String TYPE_ALL = "yyyy-MM-dd HH:mm:ss";
  public static final String TYPE_DATE = "yyyy-MM-dd";
  public static final String TYPE_DATE2 = "yyyy-MM-dd HH:mm";

  /**
   * 获取间隔多少天
   *
   * @throws ParseException
   */
  public static String getBetweenDays(String t1, String t2) throws ParseException {

    long l = getBetweenMills(t1, t2);
    long day = l / (24 * 60 * 60 * 1000);
    // long hour = (l / (60 * 60 * 1000) - day * 24);
    // long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
    // long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
    // System.out.println("" + day + "天" + hour + "小时" + min + "分" + s +
    // "秒");
    return day + "";
  }

  /**
   * 获取间隔多少毫秒
   *
   * @throws ParseException
   */
  public static long getBetweenMills(String t1, String t2) throws ParseException {
    java.util.Date now = sFormat.parse(t2);
    java.util.Date date = sFormat.parse(t1);
    long l = now.getTime() - date.getTime();
    return l;
  }

  /**
   * 获取间隔多少小时(最大值24小时)
   *
   * @throws ParseException
   */
  public static String getBetweenHours(String t1, String t2) throws ParseException {

    long l = getBetweenMills(t1, t2);
    long day = l / (24 * 60 * 60 * 1000);
    long hour = (l / (60 * 60 * 1000) - day * 24);
    // long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
    // long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
    // System.out.println("" + day + "天" + hour + "小时" + min + "分" + s +
    // "秒");
    return hour + "";
  }

  /**
   * 获取间隔多少小时(最大值24小时)
   *
   * @throws ParseException
   */
  public static String getBetweenMinites(String t1, String t2) throws ParseException {

    long l = getBetweenMills(t1, t2);
    long day = l / (24 * 60 * 60 * 1000);
    long hour = (l / (60 * 60 * 1000) - day * 24);
    long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
    // long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
    // System.out.println("" + day + "天" + hour + "小时" + min + "分" + s +
    // "秒");
    return min + "";
  }

  /**
   * 获取间隔小时
   *
   * @throws ParseException
   */
  public static String getBetweenHoursAll(String t1, String t2) throws ParseException {

    long l = getBetweenMills(t1, t2);
    // long day = l / (24 * 60 * 60 * 1000);
    long hour = (l / (60 * 60 * 1000));
    // long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
    // long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
    // System.out.println("" + day + "天" + hour + "小时" + min + "分" + s +
    // "秒");
    return hour + "";
  }

  public static String dateToString(Date date) {
    return sFormat.format(date).toString();
  }

  public static Date stringToDate(String dateString) {
    try {
      return sFormat.parse(dateString);
    } catch (ParseException e) {
      // Auto-generated catch block
      e.printStackTrace();
      return null;
    }
  }

  public static boolean before(String dateStartStr, String dateEndStr) {
    Date startDate;
    Date endDate;
    try {
      startDate = sFormat.parse(dateStartStr);
      endDate = sFormat.parse(dateEndStr);
      return startDate.before(endDate);
    } catch (ParseException e) {

      // Auto-generated catch block
      e.printStackTrace();
      return false;
    }
  }

  public static String dateToString(Date date, String type) {
    if (type == null) {
      return null;
    }
    sFormatSimple = new SimpleDateFormat(type, Locale.CHINA);
    return sFormatSimple.format(date).toString();
  }

  @Deprecated public static String dateToString(String date, String type) throws ParseException {
    java.util.Date dateNew = sFormat.parse(date);
    return dateToString(dateNew, type);
  }

  /**
   * 日期转化毫秒
   *
   * @param currTime系统当前时间 使用Context的时候传递ApplicationContext。
   */
  public static String dateToTimeInMillis(Context context, String date, Date currTime) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    String time = format.format(currTime);
    long dateMillis = 0;
    long timeMillis = 0;
    try {
      dateMillis = format.parse(date).getTime();
      timeMillis = format.parse(time).getTime();
    } catch (Exception e) {
      e.printStackTrace();
    }

    long differenceTime = timeMillis - dateMillis;

    long hour = 60 * 60 * 1000L;
    long day = 24 * 60 * 60 * 1000L;
    long month = 30 * 24 * 60 * 60 * 1000L;
    long year = 12 * 30 * 24 * 60 * 60 * 1000L;

    int months = Calendar.MONTH;
    int years = Calendar.YEAR;

    if ((0 < differenceTime) && (differenceTime < hour)) {
      return differenceTime / (1000 * 60) + context.getString(R.string.topicMinutes);
    } else if (differenceTime < day) {
      return differenceTime / (1000 * 60 * 60) + context.getString(R.string.topicHours);
    } else if (differenceTime < month) {
      return differenceTime / (1000 * 60 * 60 * 24) + context.getString(R.string.topicDays);
    } else if (differenceTime < year) {
      switch (months) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
          return differenceTime / (1000L * 60 * 60 * 24 * 31) + context.getString(
              R.string.topicMonths);
        case 4:
        case 6:
        case 9:
        case 11:
          return differenceTime / (1000L * 60 * 60 * 24 * 30) + context.getString(
              R.string.topicMonths);
        default:
          if ((years % 4 == 0 && years % 400 != 0) || years % 400 == 0) {
            return differenceTime / (1000L * 60 * 60 * 24 * 29) + context.getString(
                R.string.topicMonths);
          } else {
            return differenceTime / (1000L * 60 * 60 * 24 * 28) + context.getString(
                R.string.topicMonths);
          }
      }
    } else {
      return differenceTime / (1000L * 60 * 60 * 24 * 30 * 12) + context.getString(
          R.string.topicYears);
    }
  }

  /**
   * 获取小时和分钟的long值
   * 2015-11-30 09:00获取后为09:00的毫秒值
   *
   * @param time 字符串的时间格式 "2015-11-30 09:00"
   */
  public static long getHourAndMinute(String time) {
    String[] date = time.split(" ", time.length());
    String hourAndMinu = date[1].toString();

    String[] splitTime = hourAndMinu.split(":", hourAndMinu.length());
    long hour = Long.parseLong(splitTime[0].toString()) * (60 * 60 * 1000);
    long minutes = Long.parseLong(splitTime[1].toString()) * (60 * 1000);
    long hourAndMinute = hour + minutes;
    return hourAndMinute;
  }
}
